package com.zkr.planclass.conterllor;


import com.zkr.planclass.enummodel.StatusEnum;
import com.zkr.planclass.model.*;
import com.zkr.planclass.service.PlanClassService;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by lenovo on 2020/12/11.
 */
@RestController
@CrossOrigin

public class PlanClassConterllor {
    @Autowired
    private PlanClassService planClassService;


    //分页查询
    @PostMapping("planCategory/queryPage")
    public ResultVo queryPage(@RequestBody PlanClassSelectVo planClassSelectVo){

        return planClassService.queryPage(planClassSelectVo);
    }

    //添加
    @PostMapping("planCategory/add")
    public ResultVo add(@RequestBody @Valid PlanClassAddVo planClassAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }

        List<PlanClassAddVo> planClassAddVoList = new ArrayList<>();
        planClassAddVoList.add(planClassAddVo);
        ResultVo resultVo =  planClassService.add(planClassAddVoList);
        return resultVo;
    }

    //根据ID查看
    /*@GetMapping("queryById")
    public ResultVo queryById(Integer id){
        if (id== null){
            return ResultBuilderVo.error("查看时ID不能为空!");

        }
        return planClassService.queryById(id);
    }*/

    //修改
    @PostMapping("planCategory/update")
    public ResultVo update(@RequestBody @Valid PlanClassUpdateVo planClassUpdateVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.update(planClassUpdateVo);
    }

    //修改启用禁用状态
    @PostMapping("planCategory/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid PlanClassUpdateStatusVo planClassUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.updateStatus(planClassUpdateStatusVo);
    }

    /**
     * 下拉框
     */
    @GetMapping("metadata/getPullDownList")
    public ResultVo xiala(){
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (StatusEnum c:StatusEnum.values()){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setLabel(c.getName());
            xiaLaVo.setValue(c.getValue());
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuilderVo.success(xiaLaVoList);
    }

    /**
     * 导入
     */
   @PostMapping("quotation/InquiryIntroduction")
    public ResultVo importFile(MultipartFile file) throws Exception{
         return planClassService.importFile(file);
    }


    @GetMapping("exportFile")
    public ResultVo exportFile(String name, String code, Boolean status, HttpServletResponse response)throws Exception{
        PlanClassSelectVo planClassSelectVo = new PlanClassSelectVo();
        planClassSelectVo.setName(name);
        planClassSelectVo.setCode(code);
        planClassSelectVo.setStatus(status);
        return planClassService.exportFile(planClassSelectVo,response);
    }


}
